// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   crmDeliver_register_ok.java

package qcs.qcs;

import com.jspsmart.upload.SmartUpload;
import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;

public class crmDeliver_register_ok extends HttpServlet
{

	public crmDeliver_register_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			ServletContext context = session.getServletContext();
			String path = context.getRealPath("/");
			counter count = new counter(dbApplication);
			SmartUpload mySmartUpload = new SmartUpload();
			mySmartUpload.setCharset("UTF-8");
			nseer_db_backup1 qcs_db = new nseer_db_backup1(dbApplication);
			if (qcs_db.conn((String)dbSession.getAttribute("unit_db_name")))
				try
				{
					Date now = new Date();
					SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
					String time = formatter.format(now);
					String apply_id = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
					String crmdeliver_id = request.getParameter("div_pay_id");
					String register = request.getParameter("div_register");
					String register_id = request.getParameter("div_register_id");
					String register_time = request.getParameter("div_register_time");
					String bodyab = new String(request.getParameter("div_remark").getBytes("UTF-8"), "UTF-8");
					String remark = exchange.toHtml(bodyab);
					String sqla = (new StringBuilder("insert into qcs_apply(apply_id,crmdeliver_id,remark,register,register_id,register_time,reason) values ('")).append(apply_id).append("','").append(crmdeliver_id).append("','").append(remark).append("','").append(register).append("','").append(register_id).append("','").append(register_time).append("','3')").toString();
					qcs_db.executeUpdate(sqla);
					String product_id[] = request.getParameterValues("div_product_id");
					String product_name[] = request.getParameterValues("div_product_name");
					String stock_name[] = request.getParameterValues("div_stock_name");
					String stock_id[] = request.getParameterValues("div_stock_id");
					String amount_unit[] = request.getParameterValues("div_amount_unit");
					String demand_amount[] = request.getParameterValues("div_demand_amount");
					String label[] = request.getParameterValues("div_label");
					for (int i = 0; i < product_id.length; i++)
					{
						sqla = (new StringBuilder("insert into qcs_apply_details(apply_id,stock_id,stock_name,product_id,product_name,amount_unit,demand_amount,label,reason) values ('")).append(apply_id).append("','").append(stock_id[i]).append("','").append(stock_name[i]).append("','").append(product_id[i]).append("','").append(product_name[i]).append("','").append(amount_unit[i]).append("','").append(demand_amount[i]).append("','").append(label[i]).append("','3')").toString();
						qcs_db.executeUpdate(sqla);
						sqla = (new StringBuilder("update stock_pre_paying set qcs_apply_tag='1' where pay_id='")).append(crmdeliver_id).append("' and product_id='").append(product_id[i]).append("' and stock_name='").append(stock_name[i]).append("'").toString();
						qcs_db.executeUpdate(sqla);
					}

					sqla = (new StringBuilder("select qcs_apply_tag from stock_pre_paying where pay_id='")).append(crmdeliver_id).append("' and qcs_apply_tag='0'").toString();
					ResultSet rs = qcs_db.executeQuery(sqla);
					if (rs.next())
					{
						response.sendRedirect((new StringBuilder("qcs/qcs/crmDeliver_register.jsp?pay_id=")).append(crmdeliver_id).toString());
					} else
					{
						sqla = (new StringBuilder("update stock_pay set qcs_apply_tag='1' where pay_id='")).append(crmdeliver_id).append("'").toString();
						qcs_db.executeUpdate(sqla);
						response.sendRedirect("qcs/qcs/crmDeliver_register_ok.jsp?finished_tag=0");
					}
					qcs_db.commit();
					qcs_db.close();
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
					response.sendRedirect("qcs/qcs/crmDeliver_register_ok.jsp?finished_tag=1");
				}
			else
				response.sendRedirect("error_conn.htm");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
